<template>
  <div>
    <el-form :model="searchForm" inline>
      <el-form-item label="广告名称">
        <el-input placeholder="广告名称" clearable v-model="searchForm.name"/>
      </el-form-item>
        <el-form-item label="有效期" >
          <el-date-picker v-model="searchForm.searchExpires" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      <el-form-item label="状态">
        <el-select placeholder="选择状态" clearable v-model="searchForm.status">
          <el-option label="可用" :value="1"/>
          <el-option label="禁用" :value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button @click="openDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border>
      <el-table-column prop="id" label="编号" align="center"/>
      <el-table-column prop="name" label="用户名" align="center"/>
      <el-table-column prop="href" label="广告连接" align="center"/>
      <el-table-column prop="expiresStart" label="开始投放" align="center"/>
      <el-table-column prop="expiresEnd" label="结束投放" align="center"/>
      <el-table-column prop="target" label="跳转方式" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.target === '_self'">本页面</el-tag>
          <el-tag v-else type="warning">新页面</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="sortNo" label="排序值" width="180px">
        <template slot-scope="scope">
          <el-input v-model="scope.row.orderNo">
            <template slot="append">
              <el-button @click="changeOrder(scope.row)" size="small" icon="el-icon-check"></el-button>
            </template>
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="src" label="广告图片" align="center" width="230px">
        <template slot-scope="scope">
          <el-image @click.native="showImageDialog(scope.row.src)"
            style="width: 200px; height: 100px" :src="baseImageUrl+scope.row.src" fit="cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">可用</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="160px">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row.id)">修改</el-button>
          <el-button size="small" type="warning" @click="deleteInfo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination hide-on-single-page :current-page="pageInfo.page" :page-size="pageInfo.limit" :total="pageInfo.total" :page-sizes="[5,10,20,50,100]" layout="total,sizes,prev,pager,next,jumper" @current-change="currentChange" @size-change="sizeChange"/>
    <AdUpdate @onCloseDialog="getDataList" ref="adUpdate"/>
    <el-dialog title="图像详情" :visible.sync="showImage" v-if="showImage" custom-class="img-center">
      <div style="height: 100%;width: 100%;display: flex;align-items: center;justify-content: center;">
        <img :src="showImageUrl" alt="" style="display: block;max-height: 100%;max-width: 100%">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AdUpdate from '@/views/admin/ad/AdUpdate.vue'
export default {
  name: 'UserinfoList',
  components: { AdUpdate },
  data () {
    return {
      searchForm: {
        status: '',
        name: '',
        searchExpires: ''
      },
      pageInfo: {
        page: 1,
        limit: 5,
        total: 0
      },
      dataList: [],
      baseImageUrl: process.env.VUE_APP_BASE_URL + '/img/',
      showImage: false,
      showImageUrl: ''
    }
  },
  methods: {
    openDialog (id) {
      this.$refs.adUpdate.init(id)
    },
    deleteInfo (id) {
      this.$confirm(`确定删除编号为[${id}]的数据吗？`).then(() => {
        this.$http.delete(`ad/delete/${id}`).then(() => {
          this.$message.success('删除成功！')
          this.getDataList()
        })
      }).catch(() => {})
    },
    currentChange (val) {
      this.pageInfo.page = val
      this.getDataList()
    },
    sizeChange (val) {
      this.pageInfo.limit = val
      this.getDataList()
    },
    getDataList () {
      const params = {
        ...this.pageInfo,
        ...this.searchForm
      }
      this.$http.get('ad/page', params).then(({ data }) => {
        this.dataList = data.records
        this.pageInfo.page = data.current
        this.pageInfo.total = data.total
      })
    },
    showImageDialog (src) {
      this.showImageUrl = this.baseImageUrl + src
      this.showImage = true
    },
    changeOrder (row) {
      this.$confirm(`确定更改编号[${row.id}]的排序值为[${row.orderNo}]吗？`).then(() => {
        this.$http.post('ad/order', { id: row.id, orderNo: row.orderNo }).then(() => {
          this.$message.success('更新成功')
          this.getDataList()
        })
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.getDataList()
    })
  }
}
</script>

<style scoped>

</style>
